import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.filechooser.*;

public class EnhancedTab extends JTabbedPane {
    private ArrayList<Component> components = new ArrayList<>();
    public EnhancedTab() {
        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                int index = indexAtLocation(e.getX(), e.getY());
                if (e.getModifiers() == MouseEvent.BUTTON1_MASK) {
                    if (index > -1) setSelectedIndex(index);
                } else if (e.getModifiers() == MouseEvent.BUTTON3_MASK) {
                    JPopupMenu popupMenu = new JPopupMenu();
                    JMenuItem menuItemClose = new JMenuItem("关闭");
                    menuItemClose.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent ev) {
                            if (index > -1) {
                                removeTabAt(index);
                                components.remove(index);
                            }
                        }
                    });
                    popupMenu.add(menuItemClose);
                    JMenuItem menuItemCloseExcept = new JMenuItem("关闭其他");
                    menuItemCloseExcept.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent ev) {
                            if (index > -1) {
                                for (int i = 0; i < index; i++) {
                                    removeTabAt(0);
                                    components.remove(0);
                                }
                                int siz = components.size();
                                for (int i = 0; i < siz - 1; i++) {
                                    removeTabAt(1);
                                    components.remove(1);
                                }
                            }
                        }
                    });
                    popupMenu.add(menuItemCloseExcept);
                    JMenuItem menuItemClose2Left = new JMenuItem("关闭左边所有");
                    menuItemClose2Left.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent ev) {
                            if (index > -1) {
                                for (int i = 0; i < index; i++) {
                                    removeTabAt(0);
                                    components.remove(0);
                                }
                            }
                        }
                    });
                    popupMenu.add(menuItemClose2Left);
                    JMenuItem menuItemClose2Right = new JMenuItem("关闭右边所有");
                    menuItemClose2Right.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent ev) {
                            if (index > -1) {
                                int size = components.size();
                                for (int i = index; i < size - 1; i++) {
                                    removeTabAt(index + 1);
                                    components.remove(index + 1);
                                }
                            }
                        }
                    });
                    popupMenu.add(menuItemClose2Right);
                    popupMenu.show(EnhancedTab.this, e.getX(), e.getY());
                }
            }
        });
    }
    public void appendTab(String title, Component c) {
        components.add(c);
        addTab(title, c);
        setSelectedIndex(components.size() - 1);
    }
    public Component getSelectedComponent() {
        return getComponentAt(getSelectedIndex());
    }
    public void setSelectedTitle(String title) {
        setTitleAt(getSelectedIndex(), title);
    }
    public int componentCount() {
        return components.size();
    }
    @Override
    public void setComponentAt(int i, Component c) {
        super.setComponentAt(i, c);
        components.set(i, c);
    }
}